﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_1.Models;
using System.Data;

namespace Project_1.Controllers
{
    public class AnswerController : Controller
    {
        private const string connectionString = "Data Source=(local);Initial Catalog=AnsBook;Integrated Security=SSPI";
        AnsBookContext db = new AnsBookContext(connectionString);
        //
        // GET: /Answer/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CreateAnswer()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult CreateAnswer(CreateAnswerModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                Answer newAns = new Answer();
                newAns.Content = model.Content;
                newAns.Username = Session["Username"].ToString();
                newAns.QuestionId = int.Parse(Request.QueryString["id"].ToString());
                newAns.CreateTime = DateTime.Now;
                newAns.Dislikes = 0;
                newAns.Likes = 0;
                db.Entry(newAns).State = EntityState.Added;
                db.SaveChanges();
            }
            else
            {
                TempData["Error"] = "Your answer cannot be added, please try again later!";
            }
            return Redirect(returnUrl);
        }

    }
}
